package com.ahstu.learning.learning;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;

import com.ahstu.learning.R;

public class LearningFragement extends Fragment implements OnPageChangeListener,
        OnClickListener {
    // 设置广告栏中的fragment数量为10000，达到循环效果
    private final int ADSum = 10000;
    private ImageButton subject1, subject2, subject3, subject4, subject5,
            subject6, subject7, subject8, subject9;
    private int[] imgIds = new int[]{R.id.bannerimg1, R.id.bannerimg2,
            R.id.bannerimg3};                        //滚动栏imageview
    private int[] bannerIds = new int[]{R.drawable.banner01,
            R.drawable.banner02, R.drawable.banner03};                                                                                            //滚动栏图片
    private View view;
    private ViewPager viewPager;
    private PageFragment fragment;
    // 判断是否为手动滑动片段,默认为自动,即false
    private boolean isScolling = false;
    private Handler handler = new Handler();
    private Runnable myrunnable = new Runnable() {
        public void run() {
            if (!isScolling) {
                viewPager.setCurrentItem(viewPager.getCurrentItem() + 1);
            }
            handler.postDelayed(this, 3000);                                                    //间隔3秒
        }
    };

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.base_learning, null);
        viewPager = (ViewPager) view.findViewById(R.id.viewPager1);
        viewPager.setAdapter(new MyAdapter(getChildFragmentManager()));
        viewPager.setOnPageChangeListener(this);
        // 设置广告栏的初始化位置在中间，即左右都可以滑动
        viewPager.setCurrentItem(ADSum / 3);
        refresh();


        //课程选项按钮
        subject1 = (ImageButton) view.findViewById(R.id.subject1);
        subject2 = (ImageButton) view.findViewById(R.id.subject2);
        subject3 = (ImageButton) view.findViewById(R.id.subject3);
        subject4 = (ImageButton) view.findViewById(R.id.subject4);
        subject5 = (ImageButton) view.findViewById(R.id.subject5);
        subject6 = (ImageButton) view.findViewById(R.id.subject6);
        subject7 = (ImageButton) view.findViewById(R.id.subject7);
        subject8 = (ImageButton) view.findViewById(R.id.subject8);
        subject9 = (ImageButton) view.findViewById(R.id.subject9);

        subject1.setOnClickListener(this);
        subject2.setOnClickListener(this);
        subject3.setOnClickListener(this);
        subject4.setOnClickListener(this);
        subject5.setOnClickListener(this);
        subject6.setOnClickListener(this);
        subject7.setOnClickListener(this);
        subject8.setOnClickListener(this);
        subject9.setOnClickListener(this);

        return view;
    }

    public void onPageScrollStateChanged(int state) {
        switch (state) {
            case ViewPager.SCROLL_STATE_IDLE:
                isScolling = false;
                break;
            case ViewPager.SCROLL_STATE_DRAGGING:
                isScolling = true;
                break;
        }
    }

    public void onPageScrolled(int arg0, float arg1, int arg2) {

    }

    public void onPageSelected(int arg0) {
        for (int i = 0; i < imgIds.length; i++) {
            ImageView imageView = (ImageView) view.findViewById(imgIds[i]);
            if (arg0 % 3 == i) {
                imageView.setBackgroundResource(R.drawable.trend_dot_night);
            } else {
                imageView.setBackgroundResource(R.drawable.trend_dot_day);
            }
        }
    }

    public void refresh() {
        handler.postDelayed(myrunnable, 2000);
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
            case R.id.subject1:
                Intent chineseintent=new Intent(getActivity(), Course_list.class);
                chineseintent.putExtra("subject", "Chinese");
                startActivity(chineseintent);
                
               this.getActivity().overridePendingTransition(R.anim.enteranim,0);
                break;
            case R.id.subject2:
                Intent mathintent=new Intent(getActivity(), Course_list.class);
                mathintent.putExtra("subject", "Math");
                startActivity(mathintent);
                this.getActivity().overridePendingTransition(R.anim.enteranim,0);                break;
            case R.id.subject3:
                Intent Englishintent=new Intent(getActivity(), Course_list.class);
                Englishintent.putExtra("subject", "English");
                startActivity(Englishintent);
                this.getActivity().overridePendingTransition(R.anim.enteranim,0);                break;
            case R.id.subject4:
                Intent wuliintent=new Intent(getActivity(), Course_list.class);
                wuliintent.putExtra("subject", "Physical");
                startActivity(wuliintent);
                this.getActivity().overridePendingTransition(R.anim.enteranim,0);                break;
            case R.id.subject5:
                Intent huaxueintent=new Intent(getActivity(), Course_list.class);
                huaxueintent.putExtra("subject", "Chymist");
                startActivity(huaxueintent);
                this.getActivity().overridePendingTransition(R.anim.enteranim,0);                break;
            case R.id.subject6:
                Intent shengwuintent=new Intent(getActivity(), Course_list.class);
                shengwuintent.putExtra("subject", "Biology");
                startActivity(shengwuintent);
                this.getActivity().overridePendingTransition(R.anim.enteranim,0);                break;
            case R.id.subject7:
                Intent zhengzhiintent=new Intent(getActivity(), Course_list.class);
                zhengzhiintent.putExtra("subject", "Politics");
                startActivity(zhengzhiintent);
                this.getActivity().overridePendingTransition(R.anim.enteranim,0);                break;
            case R.id.subject8:
                Intent lishiintent=new Intent(getActivity(), Course_list.class);
                lishiintent.putExtra("subject", "History");
                startActivity(lishiintent);
                this.getActivity().overridePendingTransition(R.anim.enteranim,0);                break;
            case R.id.subject9:
                Intent diliintent=new Intent(getActivity(), Course_list.class);
                diliintent.putExtra("subject", "Geography");
                startActivity(diliintent);
                this.getActivity().overridePendingTransition(R.anim.enteranim,0);                break;
            default:
                break;

        }

    }

    class MyAdapter extends FragmentPagerAdapter {

        public MyAdapter(FragmentManager fm) {
            super(fm);
        }

        public Fragment getItem(int arg0) {
            fragment = new PageFragment();
            fragment.setPagePicture(bannerIds[arg0 % 3]);
            return fragment;
        }

        public int getCount() {
            return ADSum;
        }
    }

}
